import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import SecondaryLayout from "../../compoents/secondary-layout";
import useRouter from "../../hooks/useRouter";
import useServices from "../../hooks/useRequest";
import React, { useMemo, useState } from "react";
import { getScanRecord, getEventInfo } from "../../api/common";
import classes from "./eventRecord.module.less";
import ScrollView from "../../compoents/scroll-view";
import toast from "../../compoents/toast";
import { useLatest, useMount } from "ahooks";
import classNames from "classnames";
import EventCard from "../common/event-card";
import { dateCompare, formateDate } from "../../utils";
import { LocalKeyMap } from "../../local";
import { useTranslation } from "react-i18next";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var UsedCard = function(props) {
    return /*#__PURE__*/ _jsxs("div", {
        className: classNames(props.className, classes.used_card),
        children: [
            /*#__PURE__*/ _jsxs("div", {
                className: classes.used_head,
                children: [
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.event_title,
                        children: props.name
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.event_avatar,
                        children: /*#__PURE__*/ _jsx("img", {
                            src: props.logoUrl
                        })
                    })
                ]
            }),
            /*#__PURE__*/ _jsx("img", {
                className: classes.used_split,
                src: require("../../assets/icons/eventSplit.svg")
            }),
            /*#__PURE__*/ _jsxs("div", {
                className: classes.used_body,
                children: [
                    "Used Time: ",
                    formateDate(new Date().getTime(), "YYYY/MM/DDUTC+8 hh:mm")
                ]
            })
        ]
    });
};
function EventRecord(originProps) {
    var _useRouter = useRouter(), onaLocation = _useRouter.location;
    var props = _object_spread({}, onaLocation.query, originProps);
    var _useState = _sliced_to_array(useState([]), 2), usedRecords = _useState[0], setUsedRecords = _useState[1];
    var _useState1 = _sliced_to_array(useState([]), 2), expiredRecords = _useState1[0], setExpiredRecords = _useState1[1];
    var _useState2 = _sliced_to_array(useState("used"), 2), type = _useState2[0], setType = _useState2[1];
    var typeRef = useLatest(type);
    var t = useTranslation().t;
    var services = useServices({
        getScanRecord: getScanRecord,
        getEventInfo: getEventInfo
    });
    var list = useMemo(function() {
        if (type == "expired") return expiredRecords;
        return usedRecords;
    }, [
        usedRecords.length,
        expiredRecords.length,
        type
    ]);
    var filterExpired = function() {
        var eventData = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
        return eventData.filter(function(ev) {
            return dateCompare.isSameOrAfter(ev.currentTime, ev.endTime);
        });
    };
    var fetchRecords = function() {
        var _ref = _async_to_generator(function() {
            var usedRecords, err;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        _state.trys.push([
                            0,
                            2,
                            ,
                            3
                        ]);
                        return [
                            4,
                            services.getScanRecord({
                                tokenId: props.tokenId,
                                contract: props.contract,
                                chainType: props.chainType
                            })
                        ];
                    case 1:
                        usedRecords = _state.sent();
                        if (usedRecords.code == 200) {
                            setUsedRecords(usedRecords.data.list || []);
                            return [
                                2
                            ];
                        }
                        return [
                            3,
                            3
                        ];
                    case 2:
                        err = _state.sent();
                        toast.createToast({
                            type: "errToast",
                            message: err
                        });
                        return [
                            3,
                            3
                        ];
                    case 3:
                        return [
                            2
                        ];
                }
            });
        });
        return function fetchRecords() {
            return _ref.apply(this, arguments);
        };
    }();
    var fetchExpiredRecords = function() {
        var _ref = _async_to_generator(function() {
            var _ref, code, data, msg, err;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        _state.trys.push([
                            0,
                            2,
                            ,
                            3
                        ]);
                        return [
                            4,
                            services.getEventInfo({
                                tokenId: props.tokenId,
                                contract: props.contract,
                                chainType: props.chainType
                            })
                        ];
                    case 1:
                        _ref = _state.sent(), code = _ref.code, data = _ref.data, msg = _ref.msg;
                        if (code == 200) {
                            setExpiredRecords(filterExpired(data));
                        }
                        return [
                            3,
                            3
                        ];
                    case 2:
                        err = _state.sent();
                        toast.createToast({
                            type: "errToast",
                            message: err
                        });
                        return [
                            3,
                            3
                        ];
                    case 3:
                        return [
                            2
                        ];
                }
            });
        });
        return function fetchExpiredRecords() {
            return _ref.apply(this, arguments);
        };
    }();
    useMount(function() {
        fetchRecords();
        fetchExpiredRecords();
    });
    var Item = type === "used" ? UsedCard : EventCard;
    var _obj, _obj1;
    return /*#__PURE__*/ _jsx(SecondaryLayout, {
        title: t(LocalKeyMap.record),
        children: /*#__PURE__*/ _jsx("div", {
            className: classes.event_record,
            children: /*#__PURE__*/ _jsxs("div", {
                className: classes.event_record_container,
                children: [
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.event_record_head,
                        children: /*#__PURE__*/ _jsxs("div", {
                            className: classes.event_record_tabs,
                            children: [
                                /*#__PURE__*/ _jsx("div", {
                                    onClick: function() {
                                        setType("used");
                                    },
                                    className: classNames((_obj = {}, _define_property(_obj, classes.event_record_tab, true), _define_property(_obj, classes.event_record_tab_active, type === "used"), _obj)),
                                    children: t(LocalKeyMap.used)
                                }),
                                /*#__PURE__*/ _jsx("div", {
                                    onClick: function() {
                                        setType("expired");
                                    },
                                    className: classNames((_obj1 = {}, _define_property(_obj1, classes.event_record_tab, true), _define_property(_obj1, classes.event_record_tab_active, type === "expired"), _obj1)),
                                    children: t(LocalKeyMap.expired)
                                })
                            ]
                        })
                    }),
                    /*#__PURE__*/ _jsx(ScrollView, {
                        className: classes.event_record_body,
                        scrollY: true,
                        pullUpLoad: {
                            threshold: 0
                        },
                        renderPullUpTip: function() {
                            return null;
                        },
                        pullDownRefresh: true,
                        reload: /*#__PURE__*/ _async_to_generator(function() {
                            var fetchInstance;
                            return _ts_generator(this, function(_state) {
                                switch(_state.label){
                                    case 0:
                                        fetchInstance = typeRef.current == "used" ? fetchRecords : fetchExpiredRecords;
                                        return [
                                            4,
                                            fetchInstance()
                                        ];
                                    case 1:
                                        _state.sent();
                                        return [
                                            2
                                        ];
                                }
                            });
                        }),
                        children: list.length ? list.map(function(event) {
                            return /*#__PURE__*/ _jsx(Item, _object_spread_props(_object_spread({}, event), {
                                disabled: true
                            }), event.id);
                        }) : /*#__PURE__*/ _jsx("div", {
                            className: classes.record_empty,
                            children: /*#__PURE__*/ _jsx("span", {
                                children: t(LocalKeyMap["eventRecord.empty"])
                            })
                        })
                    })
                ]
            })
        })
    });
}
export default EventRecord;

 //# sourceMappingURL=index.js.map